package starComponent.experiments.expeComparison;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;

import javax.swing.JOptionPane;

import starComponent.experiments.expedependencies.E_Experiment;

public class TraceExperiment {

	private E_Experiment expName;
	private int groupNumber;

	private Calendar startCalendar;
	private Validator validator;

	private boolean started = false;
	
	private A_Expe2Comparison parent;

	public TraceExperiment(A_Expe2Comparison parent, E_Experiment expName, int groupNumber, String branchToObserve, double valueToValidate, int validationMode){
		this.expName = expName;
		this.parent = parent;
		this.groupNumber = groupNumber;
		this.setValidator(new Validator(this, branchToObserve, valueToValidate, validationMode));
	}

	public void start(){
		startCalendar = Calendar.getInstance();
		started = true;
	}

	public void end() throws IOException{
		if(started){
			Calendar endCalendar = Calendar.getInstance();
			
			endCalendar.add(Calendar.MINUTE, -startCalendar.get(Calendar.MINUTE));
			endCalendar.add(Calendar.SECOND, -startCalendar.get(Calendar.SECOND));
			endCalendar.add(Calendar.MILLISECOND, -startCalendar.get(Calendar.MILLISECOND));
			
			int nbMinutes = endCalendar.get(Calendar.MINUTE);
			int nbSeconds = endCalendar.get(Calendar.SECOND);
			int nbMilliseconds = endCalendar.get(Calendar.MILLISECOND);
			
			File file = new File("experimentLogs/"+
					expName.getTitle()+"/"+
					"group"+groupNumber+"/"+
					expName.getCondition()+"/"+
					expName.getScenarioTitle()+"/"+
					endCalendar.get(Calendar.DAY_OF_MONTH)+"_"+
					(endCalendar.get(Calendar.MONTH)+1)+"_"+
					endCalendar.get(Calendar.HOUR_OF_DAY)+"H_"+
					endCalendar.get(Calendar.MINUTE)+"M_"+
					endCalendar.get(Calendar.SECOND)+"S"+
					".log");
			file.mkdirs();
			
			if(file.exists())file.delete();
			file.createNewFile();
			FileWriter fw = new FileWriter(file);
			fw.write(expName.getTitle()+":\r\n\r\n");
			fw.write("Group Number = "+groupNumber+"\r\n\r\n");
			fw.write("Condition: "+expName.getCondition()+":\r\n\r\n");
			fw.write("Scenario: "+expName.getScenario()+"\r\n\r\n");
			fw.write("Success: YES \r\n\r\n");
			fw.write("Time to success: "+nbMinutes+" minutes, "+nbSeconds+" seconds, "+nbMilliseconds+" milliseconds");
			fw.close();

			System.out.println("SUCCESS !!!!");
			started = false;
			
			openSuccessPopup();
		}
	}



	public void cancel() throws IOException {
		if(started){
			Calendar endCalendar = Calendar.getInstance();
			
			endCalendar.add(Calendar.MINUTE, -startCalendar.get(Calendar.MINUTE));
			endCalendar.add(Calendar.SECOND, -startCalendar.get(Calendar.SECOND));
			endCalendar.add(Calendar.MILLISECOND, -startCalendar.get(Calendar.MILLISECOND));
			
			int nbMinutes = endCalendar.get(Calendar.MINUTE);
			int nbSeconds = endCalendar.get(Calendar.SECOND);
			int nbMilliseconds = endCalendar.get(Calendar.MILLISECOND);
			
			File file = new File("experimentLogs/"+
					expName.getTitle()+"/"+
					"group"+groupNumber+"/"+
					expName.getCondition()+"/"+
					expName.getScenarioTitle()+"/"+
					endCalendar.get(Calendar.DAY_OF_MONTH)+"_"+
					(endCalendar.get(Calendar.MONTH)+1)+"_"+
					endCalendar.get(Calendar.HOUR_OF_DAY)+"H_"+
					endCalendar.get(Calendar.MINUTE)+"M_"+
					endCalendar.get(Calendar.SECOND)+"S"+
					".log");
			file.mkdirs();
			
			if(file.exists())file.delete();
			file.createNewFile();
			FileWriter fw = new FileWriter(file);
			fw.write(expName.getTitle()+":\r\n\r\n");
			fw.write("Group Number = "+groupNumber+"\r\n\r\n");
			fw.write("Condition: "+expName.getCondition()+":\r\n\r\n");
			fw.write("Scenario: "+expName.getScenario()+"\r\n\r\n");
			fw.write("Success: NO (waited "+nbMinutes+" minutes, "+nbSeconds+" seconds, "+nbMilliseconds+" milliseconds)");
			fw.close();

			System.out.println("YOU LOST !!!!");
			started = false;
			
			openFailpopup();
		}
	}
	
	private void openFailpopup() {
		JOptionPane.showMessageDialog(parent.getFrame(), "Yes you failed !", "Result", JOptionPane.ERROR_MESSAGE);
	}

	private void openSuccessPopup() {
		JOptionPane.showMessageDialog(parent.getFrame(), "Amazing !", "Result", JOptionPane.INFORMATION_MESSAGE);
	}

	public void setValidator(Validator validator) {
		this.validator = validator;
	}

	public Validator getValidator() {
		return validator;
	}



}
